package cn.com.self.apis.portal.service.impl;

import cn.com.self.apis.portal.entity.ClassificationEntity;
import cn.com.self.apis.portal.mapper.ClassificationMapper;
import cn.com.self.apis.portal.service.intf.IClassificationService;
import cn.com.self.apis.portal.util.CustomPage;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.portal.util.ToolUtil;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CRL
 * @date 2021/1/9 14:28
 */
@Service
public class ClassificationServiceImpl extends ServiceImpl<ClassificationMapper, ClassificationEntity> implements IClassificationService {

    @Autowired
    ClassificationMapper classificationMapper;

    @Override
    public ResponseEntity creat(ClassificationEntity entity) {
        this.save(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(entity));
    }

    @Override
    public ResponseEntity deleteClassification(String id) {
        int i = classificationMapper.deleteById(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

    @Override
    public ResponseEntity updateClassification(String id, ClassificationEntity entity) {
        ClassificationEntity classificationEntity = this.getById(id);
        CopyUtils.copyProperties(entity, classificationEntity);
        this.saveOrUpdate(classificationEntity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(classificationEntity)));
    }

    @Override
    public ResponseEntity getUserPage(Map<String, Object> queryParams, Pageable page) {
        ICustomPage<ClassificationEntity> iPage = new CustomPage<>(page.getPageNumber(), page.getPageSize());
        QueryWrapper<ClassificationEntity> wrapper = new QueryWrapper<>();
        ICustomPage<ClassificationEntity> backlogEntityIPage = (ICustomPage<ClassificationEntity>)classificationMapper.selectPage(iPage, wrapper);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(backlogEntityIPage));
    }

    @Override
    public ResponseEntity loadId(String id) {
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(getById(id)));
    }


    public ClassificationEntity getById(String id) {
        Map<String, Object> map = new HashMap(1);
        map.put("id", id);
        List<ClassificationEntity> classificationEntities = classificationMapper.selectByMap(map);
        if (ToolUtil.isNotEmpty(classificationEntities)) {
            return classificationEntities.get(0);
        }
        return null;
    }

}
